package net.maku.generator.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import net.maku.generator.common.page.PageResult;
import net.maku.generator.common.query.Query;
import net.maku.generator.common.service.impl.BaseServiceImpl;
import net.maku.generator.dao.BaseClassDao;
import net.maku.generator.dao.TableDao;
import net.maku.generator.dao.TableFieldDao;
import net.maku.generator.entity.BaseClassEntity;
import net.maku.generator.entity.TableEntity;
import net.maku.generator.entity.TableFieldEntity;
import net.maku.generator.service.BaseClassService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 基类管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
public class BaseClassServiceImpl extends BaseServiceImpl<BaseClassDao, BaseClassEntity> implements BaseClassService {

    @Resource
    private TableDao tableDao;

    @Resource
    private TableFieldDao tableFieldDao;

    @Override
    public PageResult<BaseClassEntity> page(Query query) {
        IPage<BaseClassEntity> page = baseMapper.selectPage(
                getPage(query), getWrapper(query)
        );

        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<BaseClassEntity> getList() {
        return baseMapper.selectList(null);
    }

    @Override
    public Boolean syncBaseId(Long id) {
        UpdateWrapper<TableEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("baseclass_id", id); // 设置更新的字段和新值
        updateWrapper.gt("id", 0);
        tableDao.update(null, updateWrapper);
        // 自动填充INSERT、UPDATE、INSERT_UPDATE
        QueryWrapper<TableFieldEntity> queryWrapper = new QueryWrapper<>();
        BaseClassEntity byId = getById(id);
        List<String> baseEntityFields = Arrays.stream(byId.getFields().split(",")).toList();
        queryWrapper.in("field_name", baseEntityFields);
        List<TableFieldEntity> commonFields = tableFieldDao.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(commonFields)) {
            for (TableFieldEntity commonField : commonFields) {
                if (commonField.getFieldName().contains("MODITY") ||
                        commonField.getFieldName().toLowerCase().contains("update") ||
                        commonField.getFieldName().contains("MODIFY")) {
                    commonField.setAutoFill("INSERT_UPDATE");
                } else {
                    commonField.setAutoFill("INSERT");
                }
                tableFieldDao.updateById(commonField);
            }
        }
        return true;
    }

    @Override
    public boolean save(BaseClassEntity entity) {
        entity.setCreateTime(new Date());
        return super.save(entity);
    }
}